#include "Builder_Recursive.h"
#include "Log.h"

void Builder_Recursive::build(Cluster& root) {
    LOG_ENTER(TREE, "Builder_Recursive::build");

    if (root.span.n <= 1)
    {

        LOG(TREE) << "singleton node";

        // empty or singleton: do nothing.

        LOG_EXIT();

        return;
    }

    Cluster *S1,*S2; // the two sub-clusters.

    if (root.span.n == 2)
    {
        // two items.  Trivially split into two singletons.

        LOG(TREE) << "two items";

        LMO_Iterator iter = root.span.start;
        S1 = new Cluster(iter, iter, 1, 0);

        iter++;
        S2 = new Cluster(iter, iter, 1, 2);

        tree.make_family(&root, S1, S2);
    }

    else
    {   // general case.
        //
        // Make the split
        //
        splitter->split(&root, S1, S2);
        tree.make_family(&root, S1, S2);

        // and recursively split the two sub-clusters.
        build(*S1);
        build(*S2);
    }

    LOG_EXIT();
}
